<template>
  <div>
    <el-dialog
      title="修改密码"
      :visible.sync="dialogVisible"
      width="20%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input v-model="form.checkPassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleEdit">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import {editPassword} from "@/api/merchant";

  export default {
    name: "edit-password",
    data() {
      let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.form.checkPassword !== '') {
            this.$refs.form.validateField('checkPassword');
          }
          callback();
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        dialogVisible: false,
        form: {
          password: '',
          checkPassword: ''
        },
        rules: {
          password: [
            {validator: validatePass, trigger: 'blur'},
          ],
          checkPassword: [
            {validator: validatePass2, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      show() {
        this.form.password = ''
        this.form.checkPassword = ''
        this.dialogVisible = true
      },
      handleEdit() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            editPassword(this.form).then(res => {
              if (res.status === 200) {
                this.$message.success("修改成功")
                this.dialogVisible = false
              }
            })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>
